package com.gym.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.gym.pojo.User;
import com.gym.service.UserService;
import com.gym.utils.MD5Util;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;
    @RequestMapping("/listUser")
    public List<User> selListUser(String name){
        if (name==""||name==null||name.equals("")){
            name ="";
        }
        return userService.listUser(name);
    }

    @RequestMapping("/selListUser")
    public JSONObject selListUser(String name, int index, int size) {
        if (name==""||name==null||name.equals("")){
            name ="";
        }
        PageInfo<User> page=userService.selListUser(name,index,size);
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("msg","");
        json.put("count",page.getTotal());
        json.put("data",page.getList());
        System.err.println(json);
        return json;
    }

    @RequestMapping("/insUser")
    public boolean insUser(User user){
        System.out.println(user);
        user.setPassword(MD5Util.MD5(user.getUsername(),user.getPassword()));
        return userService.insUser(user);
    }

    @RequestMapping("/updUser")
    public boolean updUser(User user){
        System.out.println(user);
        return userService.updUser(user);
    }
    @RequestMapping("/selByAccount")
    public int selByAccount(String account) {
        return userService.selByAccount(account);
    }

    @RequestMapping("/delUserById")
    public boolean delUserById(int userId){
        return userService.delUserById(userId);
    }
    @RequestMapping("/provingByPwd")
    public boolean provingByPwd(int userId,String pwd,String name){
        return userService.selUserBypwd(userId, MD5Util.MD5(pwd,name));
    }
    @RequestMapping("/updUserBypwd")
    public boolean updUserBypwd(int userId, String pwd) {
        return userService.updUserBypwd(userId,pwd);
    }
    @RequestMapping("/selectsj")
    public JSONObject selectsj(String username,int userId,int page,int limit){
        PageInfo<User> pages=userService.selectsj(username,userId,page,limit);
        JSONObject json=new JSONObject();
        System.err.println(pages);
        json.put("code",0);
        json.put("msg","");
        json.put("count",pages.getTotal());
        json.put("data",pages.getList());
        return json;
    }

    @RequestMapping("/selIdByAcc")
    public int selIdByAcc(int account){
        return userService.selUserIdByAcc(account);
    }
}
